<?php
namespace Pay\Service\Alipay;

use Pay\Service\Alipay\ActionAbstract;

class Callback extends ActionAbstract
{

    /*
     * 如果没有参数config默认取自有的配置
     */
    public function __construct($config = false)
    {
        parent::__construct($config);
    }

    /*
     * 验证回调的正确性，不处理业务逻辑
     * return bool:false 验证失败,true验证成功
     * 业务中需要echo 'failure';exit;或echo 'success';exit;
     */
    public function verification($data)
    {
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = $this->config['publicKey'];
        $flag = $aop->rsaCheckV1($data, NULL, "RSA2");
        if (!$flag) {
            return false;
        }
        return $this->checkInfo($data);
    }

    protected function checkInfo($data)
    {
        if ($data['trade_status'] == 'TRADE_SUCCESS') {
            if ($data['seller_email'] != $this->config['sellerEmail']) {
                return false;
            }
            if ($data['app_id'] != $this->config['appId']) {
                return false;
            }
        }
        return true;
    }
}
?>
